import { getProjectSpaceCategory, getIslandSpaceList } from '../../../../api/index'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    projectId: {
      type: String,
      value: ''
    },
    type: {
      type: String,
      value: ''
    },
    businessType: {
      type: String,
      value: ''
    },
    businessTypeStr: {
      type: String,
      value: ''
    }
  },
  observers: {},
  data: {
    list: [],
    currentItem: {}
  },
  methods: {
    beforeOpen () {
      if (parseInt(this.data.type) === 1) {
        this.getSpaceList()
      } else {
        this.getIslandSpaceList()
      }
    },
    async getIslandSpaceList () {
      try {
        const { ResultSet } = await getIslandSpaceList({ projectId: this.data.projectId, businessTypeText: this.data.businessTypeStr, businessType: this.data.businessType, type: 3 })
        ResultSet.userData.result.forEach(item => {
          item.isSelect = false
        })
        this.setData({
          list: ResultSet.userData.result
        })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    async getSpaceList () {
      try {
        const { ResultSet } = await getProjectSpaceCategory({ projectId: this.data.projectId })
        ResultSet.userData.projectSpace.forEach(item => {
          item.isSelect = false
        })
        this.setData({
          list: ResultSet.userData.projectSpace
        })
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    select ({ currentTarget: { dataset: { item, index } } }) {
      let key = `list[${index}].isSelect`
      this.setData({ [key]: !item.isSelect })
    },
    close () {
      this.triggerEvent('close')
    },
    confirm () {
      this.triggerEvent('change', this.data.list)
    }
  },
  ready () {}
})